package com.gking.centerSystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.centerSystem.AOP.annotationUtil.desensitizeData.DesensitizeData;
import com.gking.centerSystem.AOP.annotationUtil.desensitizeData.SensitizedTypeEnum;
import com.gking.centerSystem.AOP.annotationUtil.validataParam.ValidateParam;
import com.gking.centerSystem.AOP.annotationUtil.validataParam.ValidateTypeEnum;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.AccessIpList;
import com.gking.centerSystem.service.AccessIpList.AccessIpListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/accessIpList")
public class AccessIpListController {

    @Resource
    AccessIpListService accessIpListService;


    /**
     * 添加受控 ip
     *
     * @param accessIpList 受控ip信息
     * @return m
     */
    @DesensitizeData(needVerify = true)
    @RequiresPermissions("CS:manager:set:accessIP")
    @PostMapping("/add")
    public Result<String> addAccessIpList(@RequestBody AccessIpList accessIpList) {
        if (accessIpList.getIpAddress() == null || accessIpList.getType() == null)
            return Result.error("请将参数补充完整");
        return accessIpListService.addAccessIpList(accessIpList);
    }


    /**
     * 批量删除受控 ip 项
     *
     * @param ids 要删除的id列表
     * @return m
     */
    @DesensitizeData(needVerify = true)
    @RequiresPermissions("CS:manager:set:accessIP")
    @PostMapping("/delete")
    public Result<String> deleteAccessIpList(@RequestBody List<String> ids) {
        if (ids.isEmpty()) return Result.error("请将参数补充完整");
        return accessIpListService.deleteAccessIpList(ids);
    }


    /**
     * 批量设置受控 ip 项状态
     *
     * @param ids    id 列表
     * @param enable 启用状态
     * @return m
     */
    @ValidateParam({
            @ValidateParam.Field(field = "enable", type = ValidateTypeEnum.TYPE_RequestParam, isNull = false, classType = Integer.class, message = "enable不能为空，且应为数字"),
            @ValidateParam.Field(field = "ids", isNull = false, classType = List.class, message = "id列表不能为空"),
    })
    @DesensitizeData(needVerify = true)
    @RequiresPermissions("CS:manager:set:accessIP")
    @PostMapping("/setEnable")
    public Result<String> setIpsEnableStatus(@RequestBody List<String> ids, @RequestParam Integer enable) {
        if (ids.isEmpty() || enable == null) return Result.error("请将参数补充完整");
        return accessIpListService.setIpsEnableStatus(ids, enable);
    }


    /**
     * 更新某个受控 ip 项信息
     *
     * @param accessIpList 实体类
     * @return m
     */
    @DesensitizeData(needVerify = true)
    @RequiresPermissions("CS:manager:set:accessIP")
    @PostMapping("/update")
    public Result<String> updateAccessIpListItem(@RequestBody AccessIpList accessIpList) {
        if (accessIpList.getIpAddress() == null || accessIpList.getType() == null || accessIpList.getId() == null)
            return Result.error("请将参数补充完整");
        return accessIpListService.updateAccessIpListItem(accessIpList);
    }


    /**
     * 条件分页获取受控 ip 列表
     *
     * @param page         分页页码
     * @param size         分页大小
     * @param accessIpList 查询条件
     * @return p
     */
    @DesensitizeData({
            @DesensitizeData.Field(field = "ipAddress", type = SensitizedTypeEnum.IP),
    })
    @ValidateParam({
            @ValidateParam.Field(field = "page", type = ValidateTypeEnum.TYPE_RequestParam, isNull = false, classType = Integer.class, message = "分页页码不能为空，且应为数字"),
            @ValidateParam.Field(field = "size", type = ValidateTypeEnum.TYPE_RequestParam, isNull = false, classType = Integer.class, message = "分页大小不能为空，且应为数字"),
    })
    @RequiresPermissions("CS:manager:get:accessIP")
    @PostMapping("/getList")
    public Result<Page> getAccessIpList(@RequestParam Integer page, @RequestParam Integer size, @RequestBody AccessIpList accessIpList) {
        return accessIpListService.getAccessIpList(page, size, accessIpList);
    }


    /**
     * 获取某个类型的所有 ip
     *
     * @param type 类型
     * @return l
     */
    @RequiresPermissions("CS:manager:get:accessIP")
    @PostMapping("/typeAll")
    public Result<List<AccessIpList>> getAllIpByType(@RequestParam String type) {
        return accessIpListService.getAllIpByType(type);
    }

}
